package com.hxd.otc.utils;

import android.content.Context;

import com.alibaba.verificationsdk.ui.IActivityCallback;
import com.alibaba.verificationsdk.ui.VerifyActivity;
import com.alibaba.verificationsdk.ui.VerifyType;
import com.hxd.otc.R;
import com.hxd.otc.widget.BlocksToast;

import java.util.HashMap;
import java.util.Map;

/**
 * 作  者 :  zjl
 * 时  间 :  2018/11/19 12:10
 * 描  述 :  ${投篮验证工具类}
 */
public class VerifyTypeUtils {

    private String sessionid;

    public void callSDKDoVerification(VerifyType selectedIndex, String data, final Context context, final VerifyTypeCallBack callBack) {
        IActivityCallback iActivityCallback = new IActivityCallback() {
            @Override
            //返回按钮回调
            public void onNotifyBackPressed() {
                BlocksToast.get().showToast(ResourceUtil.getString(R.string.cancel_ope));
                VerifyActivity.finishVerifyUI();
            }

            @Override
            public void onResult(int retInt, Map code) {
                verifyDidFinishedWithResult(retInt, code, callBack);
                VerifyActivity.finishVerifyUI();
            }
        };

        if (data == null) {
            VerifyActivity.startSimpleVerifyUI(context, selectedIndex, "0335", null, iActivityCallback);
        } else {
            Map<String, String> params = new HashMap<>();
            //设置params，目前只需设置 VerifyActivity.APP_DATA
            params.put(VerifyActivity.APP_DATA, data);
            VerifyActivity.startVerifyUI(context, params, selectedIndex, "0335", null, iActivityCallback);
        }
    }


    /**
     * 验签回调功能函数
     *
     * @param retInt 返回码
     * @param code   返回内容
     */
    private void verifyDidFinishedWithResult(int retInt, Map code, VerifyTypeCallBack callBack) {
        switch (retInt) {
            case VerifyActivity.VERIFY_SUCC:
                sessionid = (String) code.get("sessionID");
                LogUtils.e("验证通过:\nsessionId=" + code.get("sessionID"));
                //将得到的 sessionID 携带进接口， 去后台做校验
                if (callBack != null)
                    callBack.onSucced(sessionid);
                break;
            case VerifyActivity.VERIFY_FAILED:
                LogUtils.e((String) code.get("errorCode"));
                LogUtils.e((String) (null != code.get("errorMsg") ? code.get("errorMsg") : ""));
                BlocksToast.get().showToast((String) (null != code.get("errorMsg") ? code.get("errorMsg") : ""));
                break;
        }
    }


    public interface VerifyTypeCallBack {
        void onSucced(String sessionID);
    }

}
